package com.dyz.leetcode._10_slidingWindow;

import java.util.HashSet;
import java.util.Set;

public class _3_lengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        if(n<=1){return n;}
        int maxLen = 1;

        //双指针
        int left = 0, right=0;
        Set<Character> set = new HashSet<>();
        while (right<n){
            char rightChar = s.charAt(right);
            while(set.contains(rightChar)){
                set.remove(s.charAt(left));
                left++;
            }

            maxLen = Math.max(maxLen,  right-left+1);
            set.add(rightChar);
            right++;

        }

        return maxLen;
    }
}
